﻿; (function ($, window, documnet, undefind) {

    var defaults = {
        pick: {
            id: '#filePicker',
            label: '点击选择图片'
        },
        formData: {
        },
        resize: false, // 不压缩image    
        dnd: '#uploader .queueList',
        paste: '#uploader',
        swf: '/Content/lib/webuploader/Uploader.swf', // swf文件路径
        chunked: false,// 是否要分片处理大文件上传
        chunkSize: 2 * 1024 * 1024,// 分片上传，每片2M
        server: '/Upload/UpLoader',
        runtimeOrder: 'html5,flash',
        auto: false,//选择文件后是否自动上传
        chunkRetry: 2, //如果某个分片由于网络问题出错，允许自动重传次数
        accept: {
            title: 'Images',
            extensions: 'gif,jpg,jpeg,bmp,png',
            mimeTypes: 'image/*'
        },
        // 禁掉全局的拖拽功能。这样不会出现图片拖进页面的时候，把图片打开。
        disableGlobalDnd: true,
        fileNumLimit: 300,
        fileSizeLimit: 5*1024 * 1024 * 1024,    // 200 M
        fileSingleSizeLimit: 2 * 1024 * 1024 * 1024,   // 50 M
        fileQueued: undefind,
        uploadProgress: undefind,
        fileDequeued: undefind,
        uploadError: undefind,
        uploadFinished: undefind,
        uploadAccept: undefind,
        uploadSuccess: undefind,
        addButton: undefind,

    }
    var uploader = function (options) {

        var opt = $.extend({}, defaults, options);
        var webUploader = new WebUploader.Uploader(opt);
        if (!WebUploader.Uploader.support()) {
            alert(' 上传组件不支持您的浏览器！');
            return false;
        }
        // 拖拽时不接受 js, txt 文件。
        webUploader.on('dndAccept', function (items) {
            var denied = false,
                len = items.length,
                i = 0,
                // 修改js类型
                unAllowed = 'text/plain;application/javascript ';

            for (; i < len; i++) {
                // 如果在列表里面
                if (~unAllowed.indexOf(items[i].type)) {
                    denied = true;
                    break;
                }
            }

            return !denied;
        });

        //绑定文件加入队列事件;
        webUploader.on('fileQueued', function (file) {
            if (opt.fileQueued) {
                console.log(opt.chunked);
                if (opt.chunked) {
                    //md5计算
                    webUploader.md5File(file)
                        .progress(function (percentage) {
                            console.log('Percentage:', percentage);
                        })
                        // 完成
                        .then(function (fileMd5) { // 完成
                            var end = +new Date();
                            console.log("before-send-file  preupload: file.size=" + file.size + " file.md5=" + fileMd5);
                            file.fileMd5 = fileMd5;//获取到了md5
                            //uploader.options.formData.md5value = file.wholeMd5;//每个文件都附带一个md5，便于实现秒传

                            $('#' + file.id).find('p.error').text('MD5计算完毕');
                            console.info("MD5=" + fileMd5);
                        });
                }
                opt.fileQueued(file);
                
            }
        });
        //进度条事件
        webUploader.on('uploadProgress', function (file, percentage) {
            if (opt.uploadProgress) {
                opt.uploadProgress(file, percentage);
            }
            
        });
        
        //全部上传结束后触发;
        webUploader.on('uploadFinished', function () {
            if (opt.uploadFinished) {
                opt.uploadFinished();
            }
            
        });
        //绑定发送至服务端返回后触发事件;
        webUploader.on('uploadAccept', function (object, data) {
            if (opt.uploadAccept) {
                opt.uploadAccept(object, data);
            }
            
        });
        //上传成功后触发事件;
        webUploader.on('uploadSuccess', function (file, response) {
            if (opt.uploadSuccess) {
                opt.uploadSuccess(file, response);
            }
            
        });
        //上传失败后触发事件;
        webUploader.on('uploadError', function (file, reason) {
            if (opt.uploadError) {
                opt.uploadError(file, reason);
            }
            
        });
        //选择文件错误触发事件;
        webUploader.on('error', function (code) {
            var text = '';
            switch (code) {
                case 'F_DUPLICATE': text = '该文件已经被选择了!';
                    break;
                case 'Q_EXCEED_NUM_LIMIT': text = '上传文件数量超过限制!';
                    break;
                case 'F_EXCEED_SIZE': text = '文件大小超过限制!';
                    break;
                case 'Q_EXCEED_SIZE_LIMIT': text = '所有文件总大小超过限制!';
                    break;
                case 'Q_TYPE_DENIED': text = '文件类型不正确或者是空文件!';
                    break;
                default: text = '未知错误!';
                    break;
            }
            alert(text);
        });

        //当文件被移除队列后触发
        webUploader.on('fileDequeued', function (file) {
            console.log('fileDequeued');

            if (opt.fileDequeued) {
                opt.fileDequeued(file);
            }
        });
        if (opt.addButton) {
            webUploader.addButton(opt.addButton);
        }

        return webUploader;
    }

    window.Uploader = uploader;
}) (jQuery, window, document);